<!-- 商品分类 -->
<template>
	<view class="sort">
		<!-- 头部 -->
		<nav-bar type="fixed" :backState="2000" :bgColor="['#fff', '#fff']" :titleCenter="false" title="Open-Mall">
			<view class="Title">分类</view>
			<navigator class="search_box u-f-ac" hover-class="none" url="../common/search/search">
				<text class="iconR iconfont icon-sousuo"></text>
				<text class="prompt padding-xs">搜索</text>
			</navigator>
		</nav-bar>
		<view class="main" :style="{paddingTop: Custom + 'px'}">
			<scroll-view class="tab-view" scroll-y scroll-with-animation>
				<view class="tab-bar-item u-f-ac" v-for="(item, index) in sortList" :key="index" :class="[curIndex == index ? 'active' : '']" 
				:data-id="index" @tap.stop="TabSelect(index,item.id)">
					<text class="text-cut">{{item.name}}</text>
				</view>
			</scroll-view>
			<scroll-view scroll-y class="right-box" scroll-y scroll-with-animation 
			  @scrolltolower="getMore">
				<view class="page-view" v-if="show">
						<view class="g-container">
							<view class="g-item u-f" v-for="(item,index) in goods"  @click="Route({url:`/pages/common/goodsDetail/goodsDetail?productId=${item.id}`})">
								<image class="g-img" :src="item.mainPicture" mode="aspectFill"></image>
								<view class="g-content u-fc ">
									<view class="g-name texts-cut">{{item.productName}}</view>
									<view class="u-f-jsb u-f-ac">
										<view class="g-price text-red text-xl text-bold">￥{{item.relation.productPrice}}</view>
										<cart-control :food="item" :carList='carList' @cartChange='handelCart'></cart-control>
									</view>
								</view>
							</view>
						</view>
				</view>
				<view>
				</view>
				<view>
					<!-- <tui-loadmore v-show="loadMore"></tui-loadmore> -->
					<our-loading  :active='loadMore' :translateY='90' text=""  />
					<om-nomore :visible="noMoreFlag" bgcolor="#fff"></om-nomore>
					<tui-no-data imgUrl="https://zj.gdwrh.com/fnyg/static/images/my-img/img_nodata.png" v-if="noData1" :fixed="false">暂无内容</tui-no-data>
				</view>
			</scroll-view>
		</view>
		<shop-cart v-if="carList.length" :goods="carList" @delAll="delAll" @change='handelCart'></shop-cart>
	</view>
</template>

<script>
	import Vue from 'vue';
	export default {
		data() {
			return {
				Custom: this.CustomBar || 0,
				show: true,
				curIndex:0,
				typeId: 0,        //分类id
				sortList: [],
				goods: [],        //商品列表
				currentPage: 0,
				total: 0,
				loadMore: false,
				noMoreFlag: false,
				noData1: false,
				carList:this.$store.state.carList || []
			};
		},
		onShow() {
			if(this.goods.length == 0)return
			this.carList = this.$store.state.carList
			this.goods.forEach(item =>{
				item.count = 0
				if(!this.carList) return
				// 遍历购物车给商品列表赋值
				this.carList.forEach(car => {
					if (item.id == car.id) {
						item.count += car.count;
					}
				});
			});
		},
		onLoad() {
			// 商品分类
			this.$http.post('/api/product/categories',{}, { header: { authType: 'false'}}).then(res => {
				this.sortList = JSON.parse(res.data) || []
				this.typeId = this.sortList[0].id
				this.getProducts()
			})
		},
		methods:{
			// 根据分类id商品
			getProducts(){
				this.loadMore = true
				this.$http.post('/api/search/products/pageByType', {typeId: this.typeId,currentPage:this.currentPage}, { header: { authType: 'false'}}).then(res => {
					setTimeout(()=>{
						this.loadMore = false
					},1000)
					this.total = res.data.total
					if(this.total == 0) return this.noData1 = true
					res.data.rows.forEach(item =>{
					Vue.set(item, 'count', 0);
					if(!this.carList) return
					// 遍历购物车给商品列表赋值
					this.carList.forEach(car => {
						if (item.id == car.id) {
							item.count += car.count;
						}
					});
					})
					this.goods = [...this.goods,...res.data.rows]
				})
			},
			clear(){
				this.noData1 = false
				this.noMoreFlag = false
				this.currentPage = 0
				this.goods = []
				this.total = 0
			},
			TabSelect(index,id){
				this.show = false
				this.curIndex = index
				this.clear()
				this.typeId = id
				this.getProducts()
				this.show = true
				
			},
			// 购物车
			handelCart(item){
				this.carList = this.$store.state.carList
				this.goods.forEach(pro=>{
					if(pro.id == item.id){
						pro.count = item.count
					}
				})
			},
			delAll(){
				this.carList = []
				this.goods.forEach(item=>{
					item.count = 0
				})
			},
			// 触底加载下一页
			getMore(){
				if(this.total == this.goods.length) return this.noMoreFlag = true
				this.currentPage++
				this.getProducts()
			}
		},
		
	}
</script>

<style scoped lang="scss">
/* #ifdef APP-PLUS-NVUE */
	@function unit($num) {
		@return $num + 0rpx;
	}
/* #endif */

/* #ifndef APP-PLUS-NVUE */
	@function unit($num) {
		@return $num + 0upx;
	}
/* #endif */
// 顶部
.Title {
	margin-right: 20rpx;
}
.search_box {
	margin-right: 30upx;
	width: 450upx;
	height: 64upx;
	background-color: #f5f5f5;
	border-radius: 32upx;
	padding: 0upx 40upx;
	.prompt,.iconfont{
		font-size: 28upx;
		color: #cccccc!important;
	}
}
/* 左侧导航布局 start*/
	.main {
		height: 100%;
		width: 100%;
		position: fixed;
		top: 0;
		padding-bottom:0;
	}
	/* 隐藏scroll-view滚动条*/
	
	::-webkit-scrollbar {
		width: 0;
		height: 0;
		color: transparent;
	}
	.tab-view {
		width: 160rpx;
		background-color: #f6f6f6;
		height: 100%;
		float: left;
	}
	
	.tab-bar-item {
		width: 160rpx;
		height: 50px;
		background: #f6f6f6;
		box-sizing: border-box;
		justify-content: center;
		font-size: 26rpx;
		color: #444;
		font-weight: 400;
	}
	
	.active {
		position: relative;
		color: #000;
		font-size: 30rpx;
		font-weight: 600;
		background: #ffffff;
	}
	
	.active::before{
		content: '';
		position: absolute;
		border-left: 8rpx solid #e41f19;
		height: 30rpx;
		left: 0;
	}
	
	/* 左侧导航布局 end*/
	// 右侧商品
	.right-box {
		min-width: 100%;
		padding-left: 180rpx;
		padding-right:20rpx ;
		box-sizing: border-box;
		height: 100%;
		background-color: #fff;
		padding-top: 20rpx;
	}
	.page-view {
		width: 100%;
		overflow: hidden;
		// padding-top: 30rpx;
		box-sizing: border-box;
		padding-bottom: 200rpx;
	}
	.g-item{
		margin-bottom: 20rpx;
		min-height: 200rpx;
		.g-img{
			width: 200rpx;
			height: 200rpx;
			border-radius: 10rpx;
			align-self: center;
		}
		.g-content{
			justify-content: space-between;
			padding: 20rpx 0;
			flex: 1;
			margin-left: 18rpx;
		}
	}
	// 右侧商品
</style>
